package models

import (
	"order_srv/basic/config"
	"time"
)

// 预约订单列表结构体
type ScheduledOrderList struct {
	Id               uint32    `json:"id"`
	OrderSn          string    `json:"order_sn"`
	OrderType        int8      `json:"order_type"`
	UserId           uint32    `json:"user_id"`
	DriverId         uint32    `json:"driver_id"`
	Price            float64   `json:"price"`
	CarTypeId        int8      `json:"car_type_id"`
	StartLoc         string    `json:"start_loc"`
	EndLoc           string    `json:"end_loc"`
	PayWay           int8      `json:"pay_way"`
	OrderStatus      int8      `json:"order_status"`
	DriverDepartTime time.Time `json:"driver_depart_time"`
	DriverArriveTime time.Time `json:"driver_arrive_time"`
	ServiceStartTime time.Time `json:"service_start_time"`
	ServiceEndTime   time.Time `json:"service_end_time"`
	PayTime          time.Time `json:"pay_time"`
	CancelTime       time.Time `json:"cancel_time"`
}

// 获取用户的所有预约订单
func (o *Orders) GetUserScheduledOrders() (list []ScheduledOrderList, err error) {
	err = config.DB.Model(&Orders{}).
		Select("id,order_sn,order_type,user_id,driver_id,price,car_type_id,start_loc,end_loc,pay_way,order_status,driver_depart_time,driver_arrive_time,service_start_time,service_end_time,pay_time,cancel_time").
		Where("user_id = ? AND order_type = ?", o.UserId, 2).
		Order("service_start_time DESC").
		Find(&list).Error
	return
}

// 检查预约订单是否存在
func (o *Orders) CheckScheduledOrderExists() (bool, error) {
	var count int64
	err := config.DB.Model(&Orders{}).
		Where("id = ? AND user_id = ? AND order_type = ?", o.Id, o.UserId, 2).
		Count(&count).Error
	if err != nil {
		return false, err
	}
	return count > 0, nil
}
